package cc.chenwenxi.mypi;

import cc.chenwenxi.mypi.appium.*;
import cc.chenwenxi.mypi.appium.entity.DriverInfo;
import cc.chenwenxi.mypi.appium.kits.AppiumUtils;
import cc.chenwenxi.mypi.appium.kits.DriverKit;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import com.google.common.collect.Lists;
import io.appium.java_client.android.AndroidDriver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 将手机通过数据线连接到电脑，执行(下面的端口5555可以自行定义)：
 * adb -s xxxx tcpip 5555
 * 查看手机ip地址后，执行下面命令（替换命令中的手机IP为实际IP地址）（如果因为手机离开wifi网络导致断开，也执行此命令重新连接）
 *
 * adb connect 手机IP:5555
 * 拔掉手机数据线，就可以wifi连接手机了。
 *
 * 如果想断开连接，则执行
 * adb disconnect 手机IP:5555
 */
@Slf4j
@SpringBootApplication
public class MypiApplication {

    public static void main(String[] args) {
        SpringApplication.run(MypiApplication.class, args);
    }

    @Component
    public static class ApplicationRunnerImpl implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
//            DriverInfo cnf = new DriverInfo("redmi", "952968d30904", "8.1.0");
            DriverInfo cnf = new DriverInfo("redmi", "10.168.1.194:5555", "8.1.0");
            AndroidDriver driver = new DriverKit(cnf).genDriver();
            AppiumUtils kit = new AppiumUtils(driver);
            boolean deviceLocked = driver.isDeviceLocked();
            log.info("锁定状态:{}", deviceLocked);
            if (deviceLocked) {
                log.info("解锁");
                driver.unlockDevice();
            }

            this.shua(driver);
        }

        private void shua(AndroidDriver driver){
            AppiumTask 头条极速版 = new 头条AppiumTask(driver);
            AppiumTask 抖音极速版 = new 抖音极速版AppiumTask(driver);
            AppiumTask 快手极速版 = new 快手极速版AppiumTask(driver);
            AppiumTask 巴乐兔 = new 巴乐兔AppiumTask(driver);
            AppiumTask 掘金 = new 掘金AppiumTask(driver);

            List<AppiumTask> appiumTasks = Lists.newArrayList(头条极速版,巴乐兔,抖音极速版);
            ThreadUtil.sleep(2, TimeUnit.SECONDS);
            while (true) {
                appiumTasks.forEach(task -> {
                    if (!task.isNeedRun()) {
                        return;
                    }
                    DateTime dateTime = RandomUtil.randomDate(new Date(), DateField.MINUTE, 15, 30);
                    log.info("----任务启动:{},运行到结束时间:{}", task.appName(), DateUtil.formatDateTime(dateTime));
//                task.checkAndInstallApk();
                    log.info("{}启动", task.appName());
                    task.openActivity();
                    ThreadUtil.sleep(10, TimeUnit.SECONDS);

                    task.run(dateTime);
                });
            }

//        log.info("退出");
//        androidDriver.quit();
        }
    }
}
